﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.Text;

public partial class KhachHangDaiChung_KhachHangDaiChung_CapNhatThongTin : System.Web.UI.Page
{
    string CMND = "";//lay tu trong session
    protected void Page_Load(object sender, EventArgs e)
    {        
        //coi lai bao mat cho nay///////////
        if (Request["CMND"] == null)
            Response.Redirect("../Default.aspx");
        CMND = Request["CMND"];

        ////////////////////////////
        DataDSTableAdapters.TAIKHOANTableAdapter tkadapter = new DataDSTableAdapters.TAIKHOANTableAdapter();
        DataDS.TAIKHOANDataTable tktable= tkadapter.TAIKHOAN_LayTheoCMND(CMND);
        if (!this.IsPostBack)
        {
            chkDoiMatKhau.Checked = false;
            //txtPasswordmoi.Enabled = false;
            //txtPasswordmoinhaplai.Enabled = false;
            //txtPasswordCu.Enabled = false;
            //rqfvPasswordCu.Enabled = false;
            //rqfvPasswordMoi.Enabled = false;
            //cvPasswordNhapLai.Enabled = false;
            //txtPasswordmoi.Text = "huutho";
            ckKichHoat.Checked = tktable[0].TrangThai;
            txtHoTen.Text = (string)tktable.Rows[0]["HoTen"];
            txtEmail.Text = (string)tktable.Rows[0]["Email"];
            txtDiaChi.Text = (string)tktable.Rows[0]["DiaChi"];
            txtDienThoai.Text = tktable.Rows[0]["DienThoai"].ToString();
            //if (tktable.Rows[0]["MaTaiKhoan_NganHang"] != null)
                //txtMaTaiKhoan.Text = (string)tktable.Rows[0]["MaTaiKhoan_NganHang"].ToString();
            imgHinhAnh.ImageUrl = (string)tktable.Rows[0]["Avatar"];
            chkGuiMail.Checked=(bool)tktable.Rows[0]["Is_Mail_SP_YeuThich"];
        }
    }
    
    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        string hinhAnh = imgHinhAnh.ImageUrl;
        string imageFolder = ConfigurationSettings.AppSettings["IMAGE_FOLDER"];
        if (fulHinhAnh.FileName != "")
            hinhAnh = imageFolder + fulHinhAnh.FileName;

        DataDSTableAdapters.TAIKHOANTableAdapter tkadapter = new DataDSTableAdapters.TAIKHOANTableAdapter();
        //string matk = txtMaTaiKhoan.Text;
        //if (matk == "")
            //matk = null;
        if (chkDoiMatKhau.Checked)
        {
            //string user_oldPass = txtPasswordCu.Text;
            //string passwordmoi = txtPasswordmoi.Text;
            //string passwordmoinhaplai = txtPasswordmoinhaplai.Text;

            string passwordmoi = GlobalFunctions.MakePassword(10);
            DataDS.TAIKHOANDataTable tktable = tkadapter.TAIKHOAN_LayTheoCMND(CMND);
            
            string Salt = DateTime.Now.ToString();
            string chuoigiatrihash = GlobalFunctions.createHash(passwordmoi, Salt);

            tkadapter.TAIKHOAN_CapNhatThongTinCoPass(CMND, chuoigiatrihash, Salt, txtEmail.Text,
                txtHoTen.Text, txtDiaChi.Text, txtDienThoai.Text, hinhAnh, chkGuiMail.Checked, ckKichHoat.Checked);

            string body = "Xin chào "+txtHoTen.Text+"! \r\n";
            body += "Password của bạn ở trên hệ thống SMO đã được reset lại thành: " + passwordmoi+"\r\n";
            body += ".Vui lòng đăng nhập SMO và đổi password của bạn sớm nhất có thể.";
            try
            {
                GlobalFunctions.sendEmail(txtEmail.Text, "Reset password SMO", body);
            }
            catch (System.Exception ex)
            {
            	
            }
            

        }
        else
        {
            tkadapter.TAIKHOAN_CapNhatThongTinThongThuong(CMND, txtEmail.Text,
               txtHoTen.Text, txtDiaChi.Text, txtDienThoai.Text, hinhAnh, chkGuiMail.Checked, ckKichHoat.Checked);
        }
        Response.Write("<script>alert('Cập nhật thành công!')</script>");
        Response.Redirect("Admin_XemDanhSachTaiKhoan.aspx");
    }
    //private string createHash(string passwd, string salt)
    //{
    //    HashAlgorithm myHash = new MD5CryptoServiceProvider();
    //    string total = passwd + salt;
    //    byte[] buffer = new byte[total.Length];
    //    buffer = ASCIIEncoding.ASCII.GetBytes(total);
    //    byte[] result = myHash.ComputeHash(buffer);
    //    string pphrase = "";
    //    for (int i = 0; i < result.Length; i++)
    //        pphrase += result[i].ToString("X2");
    //    return pphrase;
    //}
    //private bool verifyPassword(string userPass, string DB_hash, string DB_salt)
    //{
    //    //Require function: createHash(string passwd, string saltValue)
    //    //Hash user input password + DB_salt
    //    string user_hash = createHash(userPass, DB_salt);
    //    if (user_hash == DB_hash)
    //        return true;
    //    return false;
    //}


}
